package com.kingtone.rongji;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kingtone.rongji.biz.CommonMessage;
import com.kingtone.rongji.network.IRequestHandler;
import com.kingtone.rongji.network.UserRequest;
import com.kingtone.rongji.tool.AppContasts;
import com.kingtone.rongji.tool.SharedPreferencesUtil;
import com.kingtone.rongji.ui.ToastManager;

public class RegisterActivity extends BaseActivity{

	private static final String TAG = "RegisterActivity";

	private Button mRegisterBtn;

	private Button mCancelBtn;

	private EditText mUserName;

	private EditText mPhone;

	private EditText mPassword;

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		this.setContentView(R.layout.activity_register);
		setupView();
	}

	private void setupView() {
		TextView title = (TextView) this.findViewById(R.id.title);
		title.setText(getString(R.string.title_reg));
		
		ImageView backBtn = (ImageView)findViewById(R.id.back);
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				onBackPressed();
			}
		});

		mUserName = (EditText) this.findViewById(R.id.id_et_username);
		mPhone = (EditText) this.findViewById(R.id.id_et_phone);
		mPassword = (EditText) this.findViewById(R.id.id_et_password);

		mRegisterBtn = (Button) this.findViewById(R.id.id_bt_submit);
		mRegisterBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String account = mUserName.getText().toString();
				String phone = mPhone.getText().toString();
				String password = mPassword.getText().toString();
				if (account.equals("")) {
					ToastManager.show(mContext, getResources().getString(
							R.string.login_username_empty_label));
					return;
				} else if (phone.equals("")) {
					ToastManager.show(mContext, getResources().getString(
							R.string.login_phone_empty_label));
					return;
				} else if (password.equals("")) {
					ToastManager.show(mContext, getResources().getString(
							R.string.login_password_empty_label));
					return;
				}
				UserRequest registerRequest = new UserRequest();
				registerRequest.register(account, password, phone, RegisterActivity.this);
			}
		});

		mCancelBtn = (Button) this.findViewById(R.id.id_bt_cancel);
		mCancelBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public void onBussinessSuccess(Object obj) {
		// TODO Auto-generated method stub
		super.onBussinessSuccess(obj);
		Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		finish();
	}

}
